/**
* @Title: evaluationRule_addOrUpdate_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2015年1月28日 上午11:06:02
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('evaluationRuleAddOrUpdateCtrl', ['$scope','$modalInstance','Modal','BaseURL','$http',
	                                       function($scope, $modalInstance,Modal,baseUrl,$http){
		
		initEvaluationTypeData();
		
		
		$scope.businessTypeList=[{id:'0',name:'全部'},{id:'1',name:'住院'},{id:'2',name:'门诊'}];
		
	　　//初始化考评类别数据集合
	　function initEvaluationTypeData(){
		var parameter={
				scoreType:$scope.categoryType //医院
		             };
		 $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'evaluationRule/initEvaluationTypeData.do',
				params :parameter
			}).success(function(response, status, headers, config) {
				 if(response){
					    var flag=response.flag;
						var list=response.list;
						 if(flag){
							 $scope.ratingCategorys=list;
						 }else{
							 Modal.alert("加载考评类别数据失败!");
						 }
				 }
			});
	}
	
	
	　//初始化考评内容数据集合
	　function initEvaluationContentData(standardTypeId){
		var parameter={
				scoreType:$scope.categoryType, //医院
				standardTypeId:standardTypeId
		             };
		 $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'evaluationRule/initEvaluationContentData.do',
				params :parameter
			}).success(function(response, status, headers, config) {
				 if(response){
					    var flag=response.flag;
						var list=response.list;
						 if(flag){
							 $scope.treeData=list;
						 }else{
							 Modal.alert("加载考评内容数据失败!");
						 }
				 }
			});
	}
		
		$scope.form = {};
		
		//选中考评类别
		$scope.changeType = function() {
			if($scope.form.idStandardscore){
				initEvaluationContentData($scope.form.idStandardscore);
			}
		}
		
		//存储左边选中的数据
		var selectData = null;
		
		//监听树形结构事件
		$scope.$on('fwaTree.selectedChange',function(newData,oldData){
			selectData = oldData ;
		});
		
		//表格的值
		$scope.selectDatas = [];
			
		//添加右边的值
		function addSelectDatas() {
			selectData.selected = false;
			
			if (selectData) {
				if (selectData 
						&& selectData.list 
						&& selectData.list.length > 0) {
						var isFalg = true;
						angular.forEach($scope.selectDatas,function(grading,i) {
							if (grading.id === selectData.id) {
								isFalg = false;
								angular.forEach(selectData.list,function(node,j) {
									var isAddSelect = true;
									angular.forEach(grading.list,function(data,k) {
										if (node.id === data.id) {
											isAddSelect = false;
										}
									});
									if (isAddSelect) {
										grading.list = grading.list || [];
										grading.list.push(node);
									}
								});
							}
						});
						if (isFalg) {
							$scope.selectDatas.push(selectData);
						}
						angular.forEach($scope.treeData,function(data,i) {
							if (data.id === selectData.id) {
								$scope.treeData.splice(i,1);
							}
						});
					}
					else {
						var isFalg = true; 
						angular.forEach($scope.selectDatas,function(grading,i) {
							if (grading.id === selectData.parentId) {
								isFalg = false;
								var isAddSelect = true;
								angular.forEach(grading.list,function(data,j) {
									if (data.id === selectData.id) {
										isAddSelect = false;
									}
								});
								if (isAddSelect) {
									grading.list = grading.list || [];
									grading.list.push(selectData);
								}
							}
						});
						if (isFalg) {
							angular.forEach($scope.treeData,function(root,i) {
								if (root.id === selectData.parentId) {
									var data = {};
									data.name = root.name;
									data.id = root.id;
									data.list = [];
									data.list.push(selectData);
									$scope.selectDatas.push(data);
								}
							});
							
						}
						angular.forEach($scope.treeData,function(node,i) {
							if (node.id === selectData.parentId) {
								angular.forEach(node.list,function(data,j) {
									if (data.id === selectData.id) {
										node.list.splice(j,1);
									}
								});
								if (!node.list || node.list.length <= 0) {
									$scope.treeData.splice(i,1);
								}
							}
						});
					}
			}
		}
		
		//删除右边的值
		function delSelectDatas() {
			delSelectData.selected = false;
			if (delSelectData) {
				if (delSelectData 
						&& delSelectData.list 
						&& delSelectData.list.length > 0) {
						var isFalg = true;
						angular.forEach($scope.treeData,function(root,i) {
							if (root.id === delSelectData.id) {
								isFalg = false;
								angular.forEach(delSelectData.list,function(node,j) {
									var isDelSelect = true;
									angular.forEach(root.list,function(data,k) {
										if (node.id === data.id) {
											isDelSelect = false;
										}
									});
									if (isDelSelect) {
										node.selected = false;
										root.list = root.list || [];
										root.list.push(node);
									}
								});
							}
						});
						if (isFalg) {
							$scope.treeData.push(delSelectData);
						}
						angular.forEach($scope.selectDatas,function(data,i) {
							if (data.id === delSelectData.id) {
								$scope.selectDatas.splice(i,1);
							}
						});
					}
					else {
						var isFalg = true; 
						angular.forEach($scope.treeData,function(root,i) {
							if (root.id === delSelectData.parentId) {
								isFalg = false;
								var isDelSelect = true;
								angular.forEach(root.list,function(data,j) {
									if (data.id === delSelectData.id) {
										isDelSelect = false;
									}
								});
								if (isDelSelect) {
									root.list = root.list || [];
									root.list.push(delSelectData);
								}
							}
						});
						if (isFalg) {
							angular.forEach($scope.selectDatas,function(grading,i) {
								if (grading.id === delSelectData.parentId) {
									var data = {};
									data.name = grading.name;
									data.id = grading.id;
									data.list = [];
									data.list.push(delSelectData);
									$scope.treeData.push(data);
								}
							});
							
						}
						angular.forEach($scope.selectDatas,function(grading,i) {
							if (grading.id === delSelectData.parentId) {
								angular.forEach(grading.list,function(data,j) {
									if (data.id === delSelectData.id) {
										grading.list.splice(j,1);
									}
								});
								if (!grading.list || grading.list.length <= 0) {
									$scope.selectDatas.splice(i,1);
								}
							}
						});
					}
			}
		}
		
		//从左边添加到右边
		$scope.add = function () {
			if (selectData) {
				if (selectData 
					&& selectData.list 
					&& selectData.list.length > 0) {
					Modal.confirm("您确定添加当前选中树形下的所有信息",function(){
						addSelectDatas();
					});
				}
				else {
					addSelectDatas();
				}
			}
			else {
				Modal.alert("请选中一项进行操作");
			}
		}
		//存储右边删除的数据
		var delSelectData = null;
		
		//从右边删除到右边
		$scope.del = function () {
			if (delSelectData) {
				if (delSelectData 
					&& delSelectData.list 
					&& delSelectData.list.length > 0) {
					Modal.confirm("您确定取消当前考核内容下的所有细则吗?",function(){
						delSelectDatas();
					});
				}
				else {
					delSelectDatas();
				}
			}
			else {
				Modal.alert("请选中一项进行操作");
			}
		}
		
		
		//表格选中事件
		$scope.selectData = function(data,isFalg) {
			delSelectData = data;
			angular.forEach($scope.selectDatas,function(grading,i){
				var  isSelected = true;
				if (isFalg) {
					if (grading.id === data.id) {
						isSelected = false;
						grading.selected = true;
						angular.forEach(grading.list,function(bean,j) {
							bean.selected = true;
						});
					}
					
				}
				else {
					if (grading.id === data.parentId) {
						isSelected = false;
						grading.selected = false;
						angular.forEach(grading.list,function(bean,j) {
							if (bean.id === data.id) {
								bean.selected = true;
							}
							else {
								bean.selected = false;
							}
						});
					}
				}
				if (isSelected) {
					grading.selected = false;
					angular.forEach(grading.list,function(bean,j) {
						bean.selected = false;
					});
				}
			});
		}
		
		
		/**
		 * 保存按钮操作
		 */
		$scope.saveConfirm=function(){
			
			if(!$scope.form.name){
				Modal.alert("请输入考评组名称!");
				return;
			}
			if(!$scope.form.idStandardscore){
				Modal.alert("考评类别不能为空!");
				return;
			}
			if(!$scope.form.businessType){
				Modal.alert("业务类型不能为空!");
				return;
			}
			if(!$scope.form.evaluationYear){
				Modal.alert("考评年度不能为空!");
				return;
			}
		
			if($scope.selectDatas.length>0){
				var catetoryType="01";//医院
				if($scope.categoryType=="0"){ //医院
					catetoryType="01"
				}else if($scope.categoryType=="2"){
					catetoryType="02"
				}
				var parameter={
						name:$scope.form.name,
						catetoryType:catetoryType,
						businessType:$scope.form.businessType,
						evaluationYear:$scope.form.evaluationYear,
						list:$scope.selectDatas
					    
		          };
				
				 $http({
						method : 'post',
						cache : false,
						url : baseUrl + 'evaluationRule/saveEvaluationRuleData.do',
						params :parameter
					}).success(function(response, status, headers, config) {
						 if(response){
							    var flag=response.flag;
								 if(flag){
									   if($scope.categoryType=="0"){ //医院
										   $scope.flashOrgCurrentPage();
										}else if($scope.categoryType=="2"){
											$scope.flashStoryCurrentPage();
										}
									
									 $scope.cancel();
								 }else{
									 Modal.alert("保存数据失败!");
								 }
						 }
					});
				
				
				
				
			}else{
				
				   Modal.alert("请选择考核内容!");
					return;
				
				
			}
			
			
			
		}
		
		
		
		//关闭弹出窗口
		$scope.cancel = function() {
			$modalInstance.dismiss();
		}
		
	}]);
})();
